import { route, GET, POST } from 'awilix-express';

@route('/customer')
export default class CustomerApi {

  constructor({ customerService }) {
    this.customerService = customerService;
  }

  @route('/getCustomerAll')
  @POST()
  async getCustomerAll(req, res){
    const {
      mark
    } = req.body
    const { success, data: list, message } = await this.customerService.getCustomerAll({mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, list);
  }

  @route('/getCustomerlist')
  @POST()
  async getCustomerlist(req, res) {
    const { PASSENGER_PASSPORT, mark, limit, offset } = req.body
    const { success, data: {count, rows}, message } = await this.customerService.getCustomerList({PASSENGER_PASSPORT, mark, limit, offset});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, {count, rows});
  }

  @route('/getCustomerOne')
  @POST()
  async getCustomerOne(req, res) {
    const { PASSENGER_PASSPORT } = req.body
    const { success, data: list, message } = await this.customerService.getCustomerOne({PASSENGER_PASSPORT});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, list);
  }

  @route('/addCustomer')
  @POST()
  async addCustomer(req, res) {
    const { 
      PASSENGER_FROM,
      PASSENGER_TO,
      PASSENGER_DATE,
      PASSENGER_CLASS,
      PASSENGER_LASTNAME,
      PASSENGER_FIRSTNAME,
      PASSENGER_PASSPORT,
      PASSENGER_VALID,
      PASSENGER_BIRTHDAY,
      PASSENGER_GENDER,
      PASSENGER_EMAIL,
      PASSENGER_PHONE1,
      PASSENGER_PHONE2,
      PASSENGER_ADDRESS,
      PASSENGER_CITY,
      PASSENGER_POSTCODE,
      mark
     } = req.body;
    const { success, message } = await this.customerService.addCustomer({
      PASSENGER_FROM,
      PASSENGER_TO,
      PASSENGER_DATE,
      PASSENGER_CLASS,
      PASSENGER_LASTNAME,
      PASSENGER_FIRSTNAME,
      PASSENGER_PASSPORT,
      PASSENGER_VALID,
      PASSENGER_BIRTHDAY,
      PASSENGER_GENDER,
      PASSENGER_EMAIL,
      PASSENGER_PHONE1,
      PASSENGER_PHONE2,
      PASSENGER_ADDRESS,
      PASSENGER_CITY,
      PASSENGER_POSTCODE,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  @route('/delCustomer')
  @POST()
  async delCustomer(req, res) {
    const { id, mark } = req.body;
    const { success, message } = await this.customerService.delCustomer({id, mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }


  @route('/updateCustomer')
  @POST()
  async updateCustomer(req, res) {
    const {
      id,
      PASSENGER_FROM,
      PASSENGER_TO,
      PASSENGER_DATE,
      PASSENGER_CLASS,
      PASSENGER_LASTNAME,
      PASSENGER_FIRSTNAME,
      PASSENGER_PASSPORT,
      PASSENGER_VALID,
      PASSENGER_BIRTHDAY,
      PASSENGER_GENDER,
      PASSENGER_EMAIL,
      PASSENGER_PHONE1,
      PASSENGER_PHONE2,
      PASSENGER_ADDRESS,
      PASSENGER_CITY,
      PASSENGER_POSTCODE
    } = req.body;
    const { success, message } = await this.customerService.updateCustomer({
      id,
      PASSENGER_FROM,
      PASSENGER_TO,
      PASSENGER_DATE,
      PASSENGER_CLASS,
      PASSENGER_LASTNAME,
      PASSENGER_FIRSTNAME,
      PASSENGER_PASSPORT,
      PASSENGER_VALID,
      PASSENGER_BIRTHDAY,
      PASSENGER_GENDER,
      PASSENGER_EMAIL,
      PASSENGER_PHONE1,
      PASSENGER_PHONE2,
      PASSENGER_ADDRESS,
      PASSENGER_CITY,
      PASSENGER_POSTCODE
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }
}